package com.douglas.managesystem.controller.student;

import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.controller.BaseController;
import com.douglas.managesystem.entity.studentpojo.Student;
import com.douglas.managesystem.service.student.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description 学生管理入口
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-15 17:52
 **/
@Controller
@RequestMapping("/stu")
public class StudentController extends BaseController {


    /**
     * 学生业务处理接口
     */
    @Autowired
    private StudentService studentService;

    /**
     * 1.分页学生列表
     * @param model
     * @param params
     * @return
     */
    @GetMapping("/student/list")
    public String list(Model model, SearchEntity params){
        PageInfo<Student> page = studentService.findAllStuByPage(params);
        model.addAttribute("student",page);
        return "admin/student/list";
    }

    /**
     * 2.去新增页面
     * @return
     */
    @GetMapping("/student/toAdd")
    public String toAdd(){
        return "admin/student/add";
    }

    /**
     * 3.去编辑页面
     * @param id
     * @return
     */
    @GetMapping("/student/toEdit")
    public String toEdit(String id,Model model){
        Student student = studentService.findStuById(id);
        model.addAttribute("student",student);
        return "admin/student/edit";
    }

    /**
     * 4.编辑或新增学生
     * @param student
     * @return
     */
    @PostMapping("/student/add")
    public String addOrEdit(Student student){
        if(StringUtils.isEmpty(student.getId())){
            studentService.saveStudent(student);
        }else{
            studentService.updateStudent(student);
        }
        return "redirect:/stu/student/list";
    }

    /**
     * 5.删除学生
     * @param id
     * @return
     */
    @GetMapping("/student/delete")
    public String delete(String id){
        studentService.deleteStudent(id);
        return "redirect:/stu/student/list";
    }

}
